/**
 * 
 */
package com.cea.papyrus.diagram.component.editpart;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.draw2d.XYLayout;
import org.eclipse.gef.EditPolicy;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Port;

import com.cea.papyrus.core.editor.ModelManager;
import com.cea.papyrus.core.editpart.DiagramEditPart;
import com.cea.papyrus.core.utils.Debug;
import com.cea.papyrus.diagraminterchange2.di2.Diagram;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;
import com.cea.papyrus.diagraminterchange2.di2.Uml1SemanticModelBridge;


/**
 * Manages model of a Component Diagram.
 * 
 * @author al090761
 *
 */
public class ComponentDiagramEditPart extends DiagramEditPart {

	/**
	 * The channel.
	 */
	private int channel = 202;

	/**
	 * The element that is associated is Diagram.
	 * 
	 * @param modelManager 
	 * @param diagram 
	 */
	public ComponentDiagramEditPart(Diagram diagram, ModelManager modelManager) {
		super(diagram, modelManager);
		Debug.debug(this,"ComponentDiagramEditPart(Diagram,ModelManager)", channel);
	}
	
	/* (non-Javadoc)
	 * @see com.cea.papyrus.common.editpart.DiagramEditPart#createEditPolicies()
	 */
	@Override
	protected void createEditPolicies(){
		super.createEditPolicies();
		installEditPolicy(EditPolicy.LAYOUT_ROLE, new ComponentLayoutEditPolicy(
				(XYLayout)getContentPane().getLayoutManager()));

	}
	@Override
	protected List getModelChildren() {
		ArrayList contained= new ArrayList();
		ArrayList ports= new ArrayList();
		ArrayList labels=new ArrayList();
		// if the presentation of the element is not shape we can display children, otherwise we can't
//		if(getGraphNode().getSemanticModel() != null) {
//			if((getGraphNode().getSemanticModel().getPresentation()==null) ||
//					!(getGraphNode().getSemanticModel().getPresentation().equals(ModelerHelper.IMAGE_STEREOTYPE_PRESENTATION))) {
				Iterator it=super.getModelChildren().iterator();
//				Contained().iterator();
				while (it.hasNext()){
					GraphElement ge=(GraphElement)it.next();
				    Element ue = null; 
				    
				    	if (!(ge instanceof GraphEdge) ) {
							if ( ((GraphNode)ge).getSemanticModel() != null ) {			    		
								if (((Uml1SemanticModelBridge) ((GraphNode)ge).getSemanticModel()).getElement() != null) {
									ue = ((Uml1SemanticModelBridge) ((GraphNode)ge).getSemanticModel()).getElement();
									if (ue instanceof  Port) {
										ports.add(ge);        // Ports will be added later to be displayed on top
									}
									else {
										contained.add(ge);
									}
								} else { //element is null (should not happen)
									//contained.add(ge);
								}					    	
							} else {  // semanticModel is null (can be a comment link for instance)
								labels.add(ge);
							}
						}
					    // if it is a graphEdge there is nothing to do
					    // GEF will take care ...		  					    		
				}
		// Now we can add ports		
		contained.addAll(labels);
		contained.addAll(ports);
		return contained;
	}
}
